package com.xnew.calendar.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.xnew.bean.common.WorkCalendarInfo;
import com.xnew.utils.DateUtils;
import com.xnew.utils.IdUtils;
import com.xnew.utils.StringUtils;
import com.xnew.calendar.mapper.WorkCalendarInfoMapper;
import com.xnew.calendar.service.WorkCalendarInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工作日维护的service信息
 * @Author yangcd
 * @Date 2020/12/31
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkCalendarInfoServiceImpl implements WorkCalendarInfoService {
    @Autowired
    private WorkCalendarInfoMapper workCalendarInfoMapper;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<WorkCalendarInfo> queryWorkCalendarList(String yearNo) throws Exception {
        return workCalendarInfoMapper.queryWorkCalendarList(yearNo);
    }

    @Override
    public void batchSaveWorkCalendar(String yearNo, List<WorkCalendarInfo> list) throws Exception {
        for(WorkCalendarInfo workCalendarInfo : list){
            workCalendarInfo.setId(IdUtils.getRandomIdByUUID());
            workCalendarInfo.setYearNo(yearNo);
        }
        workCalendarInfoMapper.batchSaveWorkCalendar(list);
    }

    @Override
    public void deleteWorkCalendarInfo(String yearNo) throws Exception {
        workCalendarInfoMapper.deleteWorkCalendarInfo(yearNo);
    }

    @Override
    public String getWorkCalendarInfo(String date, int interval) throws Exception {
        PageHelper.startPage(1, interval);
        List<String> list = workCalendarInfoMapper.getWorkCalendarInfo(date);
        if(null != list &&!list.isEmpty() && list.size() == interval){
            return list.get(interval - 1);
        }
        return "";
    }

    @Override
    public boolean ifOverTime(String date, int interval) throws Exception {
        String strDate = getWorkCalendarInfo(date,interval);
        if(StringUtils.isNotEmpty(strDate) && StringUtils.compareTo(strDate, DateUtils.dateTimeToDateString(new DateTime()))){
            return true;
        }
        return false;
    }
}
